package com.sdcx.agrobank.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionOwner;


import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements SimpleImmersionOwner {
    private Unbinder bind;
    private ImmersionBar bar;
    private boolean isPrepared;
    private boolean isLazyLoaded;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        bar = ImmersionBar.with(this);
        View view = inflater.inflate(initLayout(), container, false);
        bind = ButterKnife.bind(this, view);
        this.init(view);
        this.initData();
        this.initLogic();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lazyLoad();
        isPrepared = true;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        lazyLoad();
    }

    private void lazyLoad() {
        if (getUserVisibleHint() && isPrepared && !isLazyLoaded) {
            onLazyLoad();
            isLazyLoaded = true;
        } else {

        }
    }

    @UiThread
    public abstract void onLazyLoad();

    protected abstract int initLayout();

    //初始化
    protected abstract void init(View view);

    protected abstract void initData();

    protected abstract void initLogic();


    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
